#include "ec.h"

#include <assert.h>

const ECPoint* ECPoint_FromUInt(ECPoint *a, const UInteger *x, const UInteger *y, uint32_t z) {
	assert(a != NULL && x != NULL && y != NULL);

	UInteger_From(&a->x, x);
	UInteger_From(&a->y, y);
	a->z = z;
}


const ECPoint* ECPoint_From(ECPoint *a, const ECPoint *b) {
	assert(a != NULL && b != NULL);

	return ECPoint_FromUInt(a, &b->x, &b->y, b->z);
}

bool ECPoint_Equal(const ECPoint *a, const ECPoint *b) {
	assert( a != NULL && b != NULL);

	return UInteger_Compare(&a->x, &b->x) == 0 &&
	       UInteger_Compare(&a->y, &b->y) == 0;
}


void EC_Mul(const ECWork *ec, const UInteger *k, const ECPoint *a, ECPoint *b) {
	assert(ec != NULL && k != NULL && a != NULL && b != NULL);

	size_t bitsize = UInteger_BitSize(k);
	size_t firstbit = 0;
	for (; firstbit<bitsize; ++firstbit) {
		if (UInteger_GetBit(k, firstbit)) {
			ECPoint_From(b, a);
			break;
		}
	}

	for (size_t i=firstbit+1; i<bitsize; ++i) {
		EC_ADD(ec, b, b, b);
		if (UInteger_GetBit(k, i)) {
			EC_ADD(ec, b, a, b);
		}
	}
}
